home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gnu / adainc / a-dynpri.adb < prev    next >
Text File  |  1996-01-30  |  4KB  |  125 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                 GNU ADA RUNTIME LIBRARY (GNARL) COMPONENTS               --
  4. --                                                                          --
  5. --                 A D A . D Y N A M I C _ P R I O R I T I E S              --
  6. --                                                                          --
  7. --                                  B o d y                                 --
  8. --                                                                          --
  9. --                             $Revision: 1.5 $                             --
  10. --                                                                          --
  11. --       Copyright (c) 1991,1992,1993,1994, FSU, All Rights Reserved        --
  12. --                                                                          --
  13. -- GNARL is free software; you can redistribute it  and/or modify it  under --
  14. -- terms  of  the  GNU  Library General Public License  as published by the --
  15. -- Free Software  Foundation;  either version 2, or (at  your  option)  any --
  16. -- later  version.  GNARL is distributed  in the hope that  it will be use- --
  17. -- ful, but but WITHOUT ANY WARRANTY;  without even the implied warranty of --
  18. -- MERCHANTABILITY  or  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Gen- --
  19. -- eral Library Public License  for more details.  You should have received --
  20. -- a  copy of the GNU Library General Public License along with GNARL;  see --
  21. -- file COPYING.LIB.  If not,  write to the  Free Software Foundation,  675 --
  22. -- Mass Ave, Cambridge, MA 02139, USA.                                      --
  23. --                                                                          --
  24. ------------------------------------------------------------------------------
  25.  
  26. with System.Tasking; use System.Tasking;
  27.  
  28. with System.Tasking.Utilities;
  29. --  Used for, Utilities.ATCB_Ptr,
  30. --            Utilities.ATCB_To_ID
  31.  
  32. with System.Tasking.Stages;
  33. --  Used for, System.Tasking.Stages.Terminated
  34.  
  35. with System.Task_Primitives; use System.Task_Primitives;
  36.  
  37. with Unchecked_Conversion;
  38.  
  39. package body Ada.Dynamic_Priorities is
  40.  
  41.    function ID_To_ATCB (ID : Task_ID) return Utilities.ATCB_Ptr
  42.      renames Tasking.Utilities.ID_To_ATCB;
  43.  
  44.    function Convert_Ids is new
  45.      Unchecked_Conversion
  46.        (Task_Identification.Task_Id, System.Tasking.Task_ID);
  47.  
  48.    ------------------
  49.    -- Set_Priority --
  50.    ------------------
  51.  
  52.    --  Change base priority of a task dynamically
  53.  
  54.    procedure Set_Priority
  55.      (Priority : System.Any_Priority;
  56.       T : Ada.Task_Identification.Task_ID :=
  57.           Ada.Task_Identification.Current_Task)
  58.    is
  59.       Target : constant Utilities.ATCB_Ptr := ID_To_ATCB (Convert_Ids (T));
  60.       Source : constant Utilities.ATCB_Ptr := ID_To_ATCB (Self);
  61.       Error  : Boolean;
  62.  
  63.    begin
  64.       if Task_Identification.Is_Terminated (T) then
  65.          raise Tasking_Error;
  66.       end if;
  67.  
  68.       if T = Ada.Task_Identification.Null_Task_Id then
  69.          raise Program_Error;
  70.       end if;
  71.  
  72.       Task_Primitives.Write_Lock (Target.L, Error);
  73.  
  74.       if Source = Target then
  75.          Target.Current_Priority := Priority;
  76.          Target.Base_Priority := Priority;
  77.          System.Task_Primitives.Set_Priority (Target.LL_TCB'Access, Priority);
  78.  
  79.       else
  80.  
  81.          Target.New_Base_Priority := Priority;
  82.          Target.Pending_Priority_Change := True;
  83.          Target.Pending_Action := True;
  84.  
  85.          if Target.Suspended_Abortably then
  86.             Cond_Signal (Target.Cond);
  87.             Cond_Signal (Target.Rend_Cond);
  88.  
  89.             --  Ugly; think about ways to have tasks suspend on one
  90.             --  condition variable. ???
  91.  
  92.          end if;
  93.  
  94.          --  check for ceiling violations ???
  95.       end if;
  96.  
  97.       Task_Primitives.Unlock (Target.L);
  98.  
  99.    end Set_Priority;
  100.  
  101.    ------------------
  102.    -- Get_Priority --
  103.    ------------------
  104.  
  105.    --  Inquire base priority of a task
  106.  
  107.    function Get_Priority
  108.      (T : Ada.Task_Identification.Task_ID :=
  109.           Ada.Task_Identification.Current_Task)
  110.      return System.Any_Priority is
  111.  
  112.    begin
  113.       if Task_Identification.Is_Terminated (T) then
  114.          raise Tasking_Error;
  115.       end if;
  116.  
  117.       if T = Ada.Task_Identification.Null_Task_Id then
  118.          raise Program_Error;
  119.       end if;
  120.  
  121.       return ID_To_ATCB (Convert_Ids (T)).Base_Priority;
  122.    end Get_Priority;
  123.  
  124. end Ada.Dynamic_Priorities;
  125.